<?php
/**
 * 医院信息管理控制器
 * Created by PhpStorm.
 * User: change
 * Date: 2016/12/23
 * Time: 20:33
 */

namespace Admin\Controller;


use Admin\Model\AerainfoModel;
use Admin\Model\BaseinfoVersionModel;
use Admin\Model\CityModel;
use Admin\Model\DistrictModel;
use Base\Controller\CurdController;

class HospitalinfoController extends CurdController
{
    public function __construct()
{
    parent::__construct(true, true, array(
            'input_name' => 'coverFile',//文本框name
            'save_path' => 'hospitalcover',//保存路径
            'data_field' => 'cover'//数据库字段
        )
    );
}

    /**
     * 获取所有数据并注入模板
     * */
    protected function getAll()
    {
        if($_REQUEST['keyword']){
            $map['name'] = array('like','%'.$_REQUEST['keyword'].'%');
            $map['linkman']  = array('like', '%'.$_REQUEST['keyword'].'%');
            $map['addr']  = array('like', '%'.$_REQUEST['keyword'].'%');
            $map['description']  = array('like', '%'.$_REQUEST['keyword'].'%');
            $map['_logic'] = 'or';
            $where['_complex'] = $map;
        }
        if(!I('get.del')){
           $where['status']=array('EGT','0');
        }else{
            $where['status']=-1;
          }
        if($_REQUEST['selects']){
            $limit=$_REQUEST['selects'];
        }else{
            $limit=20;
        }
        $data = $this->model->pageToView($limit, true,$where);
        $this->assign("page", $data['page']);
        $this->assign("list", $data['list']);
    }
    /**
     * @param string $province_id
     * 根据省市获取市区的数据
     */
    public function getCityListJson($province_id = '')
    {
       $AerainfoModel= new AerainfoModel();
        $list = $AerainfoModel->relation(false)->field("aerainfo_id,aerainfo_name")->where(array('PARENT_ID'=>$province_id,'status'=>1))->select();
//       echo json_encode($list);
        $this->ajaxReturn($list);
    }
    /**
     * @param string $city_id
     * 根据市区获取县区的数据
     */
    public function getDistrictJson($city_id=""){
        $AerainfoModel= new AerainfoModel();
        $list= $AerainfoModel->relation(false)->field("aerainfo_id,aerainfo_name")->where(array('PARENT_ID'=>$city_id,'status'=>1))->select();
        $this->ajaxReturn($list);
    }
    /**
     * @param $id
     * @param int $reviewed 0拒绝 1通过
     * 审核
     */
    public function reviewed()
    {
        $id = I('get.id');
        $reviewed = I('get.status');
        $arr = explode(',', $id);
        $i = 0;
        foreach ($arr as $k => $v) {
            if (!$v) {
                continue;
            }
             $infos=$this->model->where(array('id'=>$v))->find();
            $where['status']=$reviewed;
            $where['id']=$v;
            $where['version']=$infos['version']+1;
            $this->model->save($where);
            $i++;
        }
        $this->success(($reviewed ? '已激活' : '已驳回') . $i . '条数据', $this->urlConfig['del_success']);
    }

    /**
     * 删除
     * @param string $id 要删除的id，多个以,分割
     */
    public function del($id)
    {
        $arr = explode(',', $id);
        $i = 0;
        foreach ($arr as $k => $v) {
            if (!$v) {
                continue;
            }
            $BaseinfoVersionModel=new BaseinfoVersionModel();
            $controller = hump2underline(CONTROLLER_NAME);
            $infos=$BaseinfoVersionModel->where(array('name'=>$controller))->find();
            $data['version']=$infos['version'];
            $data['status']=-1;
            $this->model->where('id=' . $v)->save($data);
            $i++;
        }
        $this->success('删除' . $i . '条数据', $this->urlConfig['del_success']);
    }
    protected function addAfter($data)
    {
        $BaseinfoVersionModel=new BaseinfoVersionModel();
        $controller = hump2underline(CONTROLLER_NAME);
        $infos=$BaseinfoVersionModel->where(array('name'=>$controller))->find();
        $data['version']=$infos['version'];
        $ids=$this->model->order('id desc')->find();
        $data['id']=$ids['id']+1;
        return $data;
    }
    protected function editAfter($data)
    {
        $BaseinfoVersionModel=new BaseinfoVersionModel();
        $controller = hump2underline(CONTROLLER_NAME);
        $infos=$BaseinfoVersionModel->where(array('name'=>$controller))->find();
        $data['version']=$infos['version'];
        return $data;
    }
}